home *** CD-ROM | disk | FTP | other *** search
- LISTING 8 - Deallocates a resource in the midst of handling an
- exception
-
- // destroy4.cpp
- #include <stdio.h>
-
- void f(char *fname);
-
- main()
- {
- try
- {
- f("file1.dat");
- }
- catch(...)
- {
- puts("Exception caught in main()");
- }
- return 0;
- }
-
- void f(char *fname)
- {
- FILE *fp = fopen(fname,"r");
- if (fp)
- {
- try
- {
- throw 1;
- }
- catch (...)
- {
- fclose(fp);
- puts("File closed");
- throw; // Re-throw
- }
-
- fclose(fp); // The normal close
- }
- }
-
- /* Output:
- File closed
- Excepion caught in main()
- */
-
-